package com.baidu.location;

import android.location.Location;
import android.net.wifi.ScanResult;
import java.io.File;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

final class an
{
  private static int A;
  private static int B;
  private static Location C;
  private static Location D;
  private static Location E;
  private static z F;
  private static String a = "baidu_location_service";
  private static ArrayList b = new ArrayList();
  private static ArrayList c = new ArrayList();
  private static ArrayList d = new ArrayList();
  private static ArrayList e = new ArrayList();
  private static ArrayList f = new ArrayList();
  private static ArrayList g = new ArrayList();
  private static String h = f.a + "/yo.dat";
  private static final String i = f.a + "/yoh.dat";
  private static final String j = f.a + "/yom.dat";
  private static final String k = f.a + "/yol.dat";
  private static final String l = f.a + "/yor.dat";
  private static File m = null;
  private static int n = 1024;
  private static int o = 512;
  private static int p = 8;
  private static int q = 5;
  private static int r = 8;
  private static int s = 16;
  private static int t = 1024;
  private static int u = 256;
  private static double v = 0.0D;
  private static double w = 0.1D;
  private static double x = 30.0D;
  private static double y = 100.0D;
  private static int z = 0;

  static
  {
    A = 64;
    B = 128;
    C = null;
    D = null;
    E = null;
    F = null;
  }

  private static int a(List paramList, int paramInt)
  {
    int i4 = 0;
    if ((paramList == null) || (paramInt > 256) || (paramInt < 0))
      return -1;
    while (true)
    {
      int i1;
      int i2;
      int i3;
      int i6;
      int i7;
      try
      {
        if (m == null)
        {
          localObject = new File(h);
          m = (File)localObject;
          if (!(((File)localObject).exists()))
          {
            ak.e();
            m = null;
            return -2;
          }
        }
        Object localObject = new RandomAccessFile(m, "rw");
        if (((RandomAccessFile)localObject).length() < 1L)
        {
          ((RandomAccessFile)localObject).close();
          return -3;
        }
        ((RandomAccessFile)localObject).seek(paramInt);
        int i5 = ((RandomAccessFile)localObject).readInt();
        i3 = ((RandomAccessFile)localObject).readInt();
        i6 = ((RandomAccessFile)localObject).readInt();
        i7 = ((RandomAccessFile)localObject).readInt();
        long l1 = ((RandomAccessFile)localObject).readLong();
        i1 = i4;
        if (i5 < 0)
          break label325;
        if (i5 < i6)
          break label336;
        i1 = i4;
        break label325:
        ((RandomAccessFile)localObject).close();
        return -4;
        byte[] arrayOfByte = new byte[t];
        i1 = p;
        if ((i1 > 0) && (i3 > 0))
        {
          ((RandomAccessFile)localObject).seek((i5 + i3 - 1) % i6 * i7 + l1);
          i4 = ((RandomAccessFile)localObject).readInt();
          if ((i4 <= 0) || (i4 >= i7))
            break label398;
          ((RandomAccessFile)localObject).read(arrayOfByte, 0, i4);
          if (arrayOfByte[(i4 - 1)] != 0)
            break label398;
          paramList.add(new String(arrayOfByte, 0, i4 - 1));
          break label398:
        }
        ((RandomAccessFile)localObject).seek(paramInt);
        ((RandomAccessFile)localObject).writeInt(i5);
        ((RandomAccessFile)localObject).writeInt(i3);
        ((RandomAccessFile)localObject).writeInt(i6);
        ((RandomAccessFile)localObject).writeInt(i7);
        ((RandomAccessFile)localObject).writeLong(l1);
        ((RandomAccessFile)localObject).close();
        paramInt = p;
        return (paramInt - i1);
      }
      catch (Exception paramList)
      {
        return -5;
      }
      while (true)
      {
        while (true)
          do
            label325: if (i1 == 0);
          while (i3 > 0);
        label336: i1 = i4;
        if (i3 < 0)
          continue;
        i1 = i4;
        if (i3 > i6)
          continue;
        i1 = i4;
        if (i6 < 0)
          continue;
        i1 = i4;
        if (i6 > 1024)
          continue;
        i1 = i4;
        if (i7 < 128)
          continue;
        i1 = i4;
        if (i7 > 1024)
          continue;
        i2 = 1;
      }
      label398: i2 -= 1;
      i3 -= 1;
    }
  }

  public static String a()
  {
    Object localObject;
    int i1 = 1;
    String str = null;
    if (i1 < 5)
      if (i1 == 1)
      {
        str = i;
        localObject = e;
        if (localObject != null)
          break label94;
      }
    for (str = null; ; str = null)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                localObject = str;
                if (str != null)
                  break label291;
                i1 += 1;
              }
              if (i1 != 2)
                break;
              str = j;
              localObject = f;
            }
            if (i1 != 3)
              break;
            str = k;
            localObject = g;
          }
          if (i1 != 4)
            break;
          str = l;
          localObject = g;
        }
        str = null;
        continue;
        if (((List)localObject).size() <= 0)
        {
          label94: new StringBuilder().append(str).append(":get data from sd file...");
          ak.e();
          a(str, (List)localObject);
        }
        if (((List)localObject).size() <= 0)
          break label293;
        str = (String)((List)localObject).get(0);
        ((List)localObject).remove(0);
      }
      ak.e();
      a(g, A);
      if (g.size() > 0)
      {
        str = (String)g.get(0);
        g.remove(0);
      }
      localObject = str;
      if (str == null)
      {
        a(g, z);
        if (g.size() > 0)
        {
          str = (String)g.get(0);
          g.remove(0);
        }
        localObject = str;
        if (str == null)
        {
          a(g, B);
          localObject = str;
          if (g.size() > 0)
          {
            localObject = (String)g.get(0);
            g.remove(0);
          }
        }
      }
      label291: label293: return localObject;
    }
  }

  public static void a(double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4)
  {
    if (paramDouble1 > 0.0D)
    {
      v = paramDouble1;
      w = paramDouble2;
      if (paramDouble3 <= 20.0D)
        break label41;
    }
    while (true)
    {
      while (true)
      {
        x = paramDouble3;
        y = paramDouble4;
        return;
        paramDouble1 = v;
      }
      label41: paramDouble3 = x;
    }
  }

  private static void a(int paramInt, boolean paramBoolean)
  {
    label160: int i2;
    int i3;
    int i4;
    int i5;
    Object localObject1;
    Object localObject2;
    Object localObject3;
    if (paramInt == 1)
    {
      localObject2 = i;
      if (paramBoolean)
        return;
      localObject1 = e;
      localObject3 = new File((String)localObject2);
      if (((File)localObject3).exists());
    }
    try
    {
      localObject2 = new File((String)localObject2);
      if (!(((File)localObject2).exists()))
      {
        File localFile = new File(f.a);
        if (!(localFile.exists()))
          localFile.mkdirs();
        if (((File)localObject2).createNewFile())
          break label645;
        localObject2 = null;
        localObject2 = new RandomAccessFile((File)localObject2, "rw");
        ((RandomAccessFile)localObject2).seek(0L);
        ((RandomAccessFile)localObject2).writeInt(32);
        ((RandomAccessFile)localObject2).writeInt(2048);
        ((RandomAccessFile)localObject2).writeInt(1040);
        ((RandomAccessFile)localObject2).writeInt(0);
        ((RandomAccessFile)localObject2).writeInt(0);
        ((RandomAccessFile)localObject2).writeInt(0);
        ((RandomAccessFile)localObject2).close();
      }
      try
      {
        do
        {
          int i1;
          int i7;
          while (true)
          {
            do
            {
              label404: label450: 
              do
                while (true)
                {
                  while (true)
                  {
                    while (true)
                    {
                      while (true)
                      {
                        localObject2 = new RandomAccessFile((File)localObject3, "rw");
                        ((RandomAccessFile)localObject2).seek(4L);
                        int i6 = ((RandomAccessFile)localObject2).readInt();
                        i7 = ((RandomAccessFile)localObject2).readInt();
                        i2 = ((RandomAccessFile)localObject2).readInt();
                        i3 = ((RandomAccessFile)localObject2).readInt();
                        i1 = ((RandomAccessFile)localObject2).readInt();
                        for (i4 = ((List)localObject1).size(); ; i4 = i5)
                        {
                          if (i4 <= r)
                            break label639;
                          ak.e();
                          if (!(paramBoolean))
                            break label636;
                          i1 += 1;
                          if (i2 >= i6)
                            break label472;
                          ((RandomAccessFile)localObject2).seek(i7 * i2 + 128);
                          localObject3 = ((String)((List)localObject1).get(0)) + '\0'.getBytes();
                          ((RandomAccessFile)localObject2).writeInt(localObject3.length);
                          ((RandomAccessFile)localObject2).write(localObject3, 0, localObject3.length);
                          ((List)localObject1).remove(0);
                          i5 = i2 + 1;
                          i2 = i3;
                          i3 = i5;
                          i5 = i4 - 1;
                          i4 = i3;
                          i3 = i2;
                          i2 = i4;
                        }
                        if (paramInt != 2)
                          break label404;
                        localObject1 = j;
                        if (!(paramBoolean))
                          break;
                        localObject3 = e;
                        localObject2 = localObject1;
                        localObject1 = localObject3;
                      }
                      localObject3 = f;
                      localObject2 = localObject1;
                      localObject1 = localObject3;
                    }
                    if (paramInt != 3)
                      break label450;
                    localObject1 = k;
                    if (!(paramBoolean))
                      break;
                    localObject3 = f;
                    localObject2 = localObject1;
                    localObject1 = localObject3;
                  }
                  localObject3 = g;
                  localObject2 = localObject1;
                  localObject1 = localObject3;
                }
              while (paramInt != 4);
              localObject2 = l;
            }
            while (!(paramBoolean));
            localObject1 = g;
          }
          label472: if (!(paramBoolean))
            break label661;
          long l1 = i3 * i7 + 128;
          ((RandomAccessFile)localObject2).seek(l1);
          localObject3 = ((String)((List)localObject1).get(0)) + '\0'.getBytes();
          ((RandomAccessFile)localObject2).writeInt(localObject3.length);
          ((RandomAccessFile)localObject2).write(localObject3, 0, localObject3.length);
          ((List)localObject1).remove(0);
          i5 = i3 + 1;
          i3 = i5;
          if (i5 <= i2)
            break label648;
          i3 = 0;
          break label648:
          ((RandomAccessFile)localObject2).seek(12L);
          ((RandomAccessFile)localObject2).writeInt(i2);
          ((RandomAccessFile)localObject2).writeInt(i3);
          ((RandomAccessFile)localObject2).writeInt(i1);
          ((RandomAccessFile)localObject2).close();
        }
        while ((i4 == 0) || (paramInt >= 4));
        a(paramInt + 1, true);
        label645: label648: label661: label636: label639: return;
      }
      catch (Exception localException1)
      {
        return;
      }
    }
    catch (Exception localException2)
    {
      while (true)
      {
        while (true)
          while (true)
          {
            while (true)
              break label160:
            i4 = 0;
          }
        i5 = i2;
        i2 = i3;
        i3 = i5;
      }
      i4 = 1;
    }
  }

  public static void a(u paramu, z paramz, Location paramLocation, String paramString)
  {
    z localz = null;
    Object localObject = null;
    if (!(ak.Z));
    do
    {
      do
      {
        do
          return;
        while ((ak.p == 3) && (!(a(paramLocation, paramz))) && (!(a(paramLocation, false))));
        if ((paramu == null) || (!(paramu.a())))
          break label101;
        localz = paramz;
        if (!(a(paramLocation, paramz)))
          localz = null;
        paramu = ak.a(paramu, localz, paramLocation, paramString, 1);
      }
      while (paramu == null);
      a(Jni.a(paramu));
      D = paramLocation;
      C = paramLocation;
    }
    while (localz == null);
    F = localz;
    return;
    if ((paramz != null) && (paramz.i()) && (a(paramLocation, paramz)))
    {
      label101: if (a(paramLocation))
        break label254;
      paramu = localObject;
    }
    while (true)
    {
      do
      {
        do
          paramu = ak.a(paramu, paramz, paramLocation, paramString, 2);
        while (paramu == null);
        paramu = Jni.a(paramu);
        new StringBuilder("upload size:").append(paramu.length());
        ak.e();
        a(paramu);
        E = paramLocation;
        C = paramLocation;
      }
      while (paramz == null);
      F = paramz;
      return;
      if (!(a(paramLocation)))
        paramu = null;
      if (!(a(paramLocation, paramz)))
        paramz = localz;
      while (true)
      {
        do
        {
          do
          {
            if ((paramu != null) || (paramz != null));
            paramu = ak.a(paramu, paramz, paramLocation, paramString, 3);
          }
          while (paramu == null);
          a(Jni.a(paramu));
          C = paramLocation;
        }
        while (paramz == null);
        F = paramz;
        label254: return;
      }
    }
  }

  private static void a(String paramString)
  {
    int i1 = ak.k;
    if (i1 == 1);
    for (ArrayList localArrayList = e; localArrayList == null; localArrayList = g)
      do
        while (true)
        {
          return;
          if (i1 != 2)
            break;
          localArrayList = f;
        }
      while (i1 != 3);
    ak.e();
    if (localArrayList.size() <= s)
      localArrayList.add(paramString);
    if (localArrayList.size() >= s)
      a(i1, false);
    while (true)
    {
      if (localArrayList.size() > s);
      localArrayList.remove(0);
    }
  }

  private static boolean a(Location paramLocation)
  {
    double d1;
    double d2;
    double d3;
    double d4;
    if (paramLocation == null);
    do
    {
      return false;
      if ((D == null) || (C == null))
      {
        D = paramLocation;
        return true;
      }
      d1 = paramLocation.distanceTo(D);
      d2 = ak.M;
      d3 = ak.N;
      d4 = ak.O;
    }
    while (paramLocation.distanceTo(C) <= d1 * d3 + d2 * d1 * d1 + d4);
    return true;
  }

  private static boolean a(Location paramLocation, z paramz)
  {
    int i2;
    if ((paramLocation == null) || (paramz == null) || (paramz.a == null) || (paramz.a.isEmpty()))
      return false;
    z localz = F;
    if ((paramz.a != null) && (localz != null) && (localz.a != null))
      if (paramz.a.size() < localz.a.size())
      {
        i1 = paramz.a.size();
        i2 = 0;
        if (i2 >= i1)
          break label213;
        String str1 = ((ScanResult)paramz.a.get(i2)).BSSID;
        int i3 = ((ScanResult)paramz.a.get(i2)).level;
        String str2 = ((ScanResult)localz.a.get(i2)).BSSID;
        int i4 = ((ScanResult)localz.a.get(i2)).level;
        if ((str1.equals(str2)) && (i3 == i4))
          break label206;
      }
    for (int i1 = 0; i1 != 0; i1 = 0)
      while (true)
      {
        while (true)
        {
          while (true)
          {
            return false;
            i1 = localz.a.size();
          }
          label206: i2 += 1;
        }
        label213: i1 = 1;
      }
    if (E == null)
    {
      E = paramLocation;
      return true;
    }
    return true;
  }

  public static boolean a(Location paramLocation, boolean paramBoolean)
  {
    return n.a(C, paramLocation, paramBoolean);
  }

  private static boolean a(String paramString, List paramList)
  {
    paramString = new File(paramString);
    if (!(paramString.exists()))
      return false;
    while (true)
    {
      int i1;
      int i2;
      int i3;
      int i7;
      try
      {
        long l1;
        paramString = new RandomAccessFile(paramString, "rw");
        paramString.seek(8L);
        int i5 = paramString.readInt();
        i1 = paramString.readInt();
        i4 = paramString.readInt();
        byte[] arrayOfByte = new byte[t];
        i2 = r;
        i2 += 1;
        i6 = 0;
        if ((i2 > 0) && (i1 > 0))
        {
          i3 = i4;
          if (i1 < i4)
            i3 = 0;
          l1 = (i1 - 1) * i5 + 128;
        }
        try
        {
          paramString.seek(l1);
          i4 = paramString.readInt();
          i7 = i6;
          if (i4 <= 0)
            break label220;
          i7 = i6;
          if (i4 >= i5)
            break label220;
          paramString.read(arrayOfByte, 0, i4);
          i7 = i6;
          if (arrayOfByte[(i4 - 1)] != 0)
            break label220;
          paramList.add(new String(arrayOfByte, 0, i4 - 1));
          i7 = 1;
          break label220:
          paramString.seek(12L);
          paramString.writeInt(i1);
          paramString.writeInt(i4);
          paramString.close();
          return i6;
        }
        catch (Exception paramString)
        {
          return i6;
        }
      }
      catch (Exception paramString)
      {
        return false;
      }
      label220: i2 -= 1;
      i1 -= 1;
      int i6 = i7;
      int i4 = i3;
    }
  }

  public static void b()
  {
    r = 0;
    ak.e();
    a(1, false);
    a(2, false);
    a(3, false);
    r = 8;
  }

  public static String c()
  {
    int i1;
    Object localObject2;
    Object localObject4 = null;
    String str = null;
    Object localObject5 = new File(j);
    Object localObject1 = localObject4;
    if (((File)localObject5).exists())
      localObject1 = str;
    try
    {
      localObject5 = new RandomAccessFile((File)localObject5, "rw");
      localObject1 = str;
      ((RandomAccessFile)localObject5).seek(20L);
      localObject1 = str;
      i1 = ((RandomAccessFile)localObject5).readInt();
      localObject1 = localObject4;
      if (i1 <= 128)
        break label118;
      localObject1 = str;
      str = "&p1=" + i1;
      localObject1 = str;
      ((RandomAccessFile)localObject5).seek(20L);
      localObject1 = str;
      ((RandomAccessFile)localObject5).writeInt(0);
      localObject1 = str;
      ((RandomAccessFile)localObject5).close();
      localObject1 = str;
      label221: label118: return localObject1;
    }
    catch (Exception localException3)
    {
      localObject4 = new File(k);
      Object localObject3 = localObject1;
      if (((File)localObject4).exists())
        localObject3 = localObject1;
      try
      {
        localObject4 = new RandomAccessFile((File)localObject4, "rw");
        localObject3 = localObject1;
        ((RandomAccessFile)localObject4).seek(20L);
        localObject3 = localObject1;
        i1 = ((RandomAccessFile)localObject4).readInt();
        localObject3 = localObject1;
        if (i1 <= 256)
          break label221;
        localObject3 = localObject1;
        localObject1 = "&p2=" + i1;
        localObject3 = localObject1;
        ((RandomAccessFile)localObject4).seek(20L);
        localObject3 = localObject1;
        ((RandomAccessFile)localObject4).writeInt(0);
        localObject3 = localObject1;
        ((RandomAccessFile)localObject4).close();
        return localObject1;
      }
      catch (Exception localObject2)
      {
        do
        {
          localObject4 = new File(l);
          localObject2 = localObject3;
        }
        while (!(((File)localObject4).exists()));
        localObject2 = localObject3;
        try
        {
          do
          {
            localObject4 = new RandomAccessFile((File)localObject4, "rw");
            localObject2 = localObject3;
            ((RandomAccessFile)localObject4).seek(20L);
            localObject2 = localObject3;
            i1 = ((RandomAccessFile)localObject4).readInt();
            localObject2 = localObject3;
          }
          while (i1 <= 512);
          localObject2 = localObject3;
          localObject3 = "&p3=" + i1;
          localObject2 = localObject3;
          ((RandomAccessFile)localObject4).seek(20L);
          localObject2 = localObject3;
          ((RandomAccessFile)localObject4).writeInt(0);
          localObject2 = localObject3;
          ((RandomAccessFile)localObject4).close();
          return localObject3;
        }
        catch (Exception localException3)
        {
        }
      }
    }
    return ((String)(String)(String)(String)localObject2);
  }
}